上次项目中使用事务遇到一个问题:

java.lang.IllegalStateException: Cannot activate transaction synchronization - already active  
at org.springframework.transaction.support.TransactionSynchronizationManager.initSynchronization(TransactionSynchronizationManager.java:270)  
at org.springframework.transaction.support.AbstractPlatformTransactionManager.prepareSynchronization(AbstractPlatformTransactionManager.java:537)  
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)  
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:417)  
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:255)  
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)  
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)  
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)  
at $Proxy10.createOffersInBatch(Unknown Source)  
at com.alibaba.cobar.client.transaction.MultipleDataSourcesTransactionManagerTest.testOfferCreationOnMultipleShardsWithNormallyOfferService(MultipleDataSourcesTransactionManagerTest.java:94) 

翻译过来就是:无法激活事务同步-已激活

一开始找代码的bug,始终发现不了什么毛病,然后就Google了一番,原来我的项目用了多数据源,使用了Cobar来分表分库,initSynchronization 重复初始化,导致异常。

解决方法:

<bean id="txManager" class="com.alibaba.cobar.client.transaction.MultipleDataSourcesTransactionManager">
    <property name="cobarDataSourceService" ref="dataSources"/>
    <property name="transactionSynchronization" value="2" />
</bean>

参考文章 http://chinalibra.iteye.com/b...


野咖
23 声望0 粉丝